package com.example.repair.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.repair.R;
import com.example.repair.base.BaseActivity;
import com.example.repair.base.Presenter.PressenterImpl;
import com.example.repair.base.netWork.Constant;
import com.example.repair.base.netWork.LoginContract;
import com.example.repair.entity.Bean;
import com.example.repair.entity.CheckUser;
import com.example.repair.util.SpUtils;
import com.example.repair.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.tishi)
    TextView tishi;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.edit_image)
    EditText editImage;
    @BindView(R.id.code_image)
    WebView codeImage;
    @BindView(R.id.code_text)
    TextView codeText;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.getcode)
    TextView getcode;
    @BindView(R.id.next_type)
    Button nextType;



    private PressenterImpl pressenter;


    private int djs = 60;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            getcode.setClickable(false);
            djs--;
            if (djs <= 0) {
                getcode.setText("重新获取验证码");
                getcode.setClickable(true);
            } else {
                getcode.setText(djs + "s后重新获取");
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };



    @Override
    protected int getLayout() {
        return R.layout.activity_forget;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initData() {

        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        codeImage.loadUrl("http://qyt.dhmbj.net/appapi/treat/verify_test.html");
        phone.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                // et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
                Drawable drawable = phone.getCompoundDrawables()[2];
                //如果右边没有图片，不再处理
                if (drawable == null) {
                    return false;
                }
                //如果不是按下事件，不再处理
                if (motionEvent.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (motionEvent.getX() > phone.getWidth() - phone.getPaddingRight() - drawable.getIntrinsicWidth()) {
                    phone.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);//画在左边
                    phone.setText("");
                }
                return false;
            }
        });


    }

    @OnClick({R.id.back, R.id.code_text, R.id.getcode, R.id.next_type})
    public void onViewClicked(View view) {
        Map<String,String> map;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.code_text:
                codeImage.loadUrl(Constant.PATH+"/appapi/treat/verify_test.html");
                break;
            case R.id.getcode:

                String phones = phone.getText().toString();
                String edit_images = editImage.getText().toString();

                if (phones == null || phones.equals("")) {
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.isRegistMobileNO(phones)) {
                    Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (edit_images==null||edit_images.equals("")){
                    Toast.makeText(this, "请输入图形验证码", Toast.LENGTH_SHORT).show();
                    return;
                }


                map = new HashMap<>();
                map.put("mobile", phones);
                map.put("token", Constant.TOKEN);
                map.put("pictureVerfy",edit_images);
                pressenter.sendMessage(this, Constant.send_message, map, Bean.class);

                break;
            case R.id.next_type:


                String mobile = phone.getText().toString();
                String pictureVerfy = editImage.getText().toString();
                String codes = code.getText().toString();

                if (mobile==null||mobile.equals("")){
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (pictureVerfy==null||pictureVerfy.equals("")){
                    Toast.makeText(this, "请输入图形验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (codes==null||codes.equals("")){
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.isRegistMobileNO(mobile)){
                    Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }

                map=new HashMap<>();
                map.put("token",Constant.TOKEN);
                map.put("mobile",mobile);
                map.put("pictureVerfy",pictureVerfy);
                map.put("code",codes);

                pressenter.sendMessage(this,Constant.check_user,map, CheckUser.class);

                break;
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof Bean) {
            Bean bean = (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();

            if (bean.getCode() == 1) {
                getcode.setClickable(false);
                djs = 60;
                handler.sendEmptyMessageDelayed(0, 1000);
            }else {
                codeImage.loadUrl(Constant.PATH+"/appapi/treat/verify_test.html");
            }
        }else if (data instanceof CheckUser){
            CheckUser checkUser= (CheckUser) data;
            Toast.makeText(this, checkUser.getMessage(), Toast.LENGTH_SHORT).show();
            if (checkUser.getCode()==1){

                SpUtils.saveString(this,"uid",checkUser.getData().get(0).getUid()+"");

                Intent intent=new Intent(this,ResetActivity.class);
                startActivity(intent);
                finish();
            }

        }
    }

    @Override
    public void fail(String error) {

    }
}
